﻿namespace Jsl.FxCop
{
    using System.Collections;
    using System.Reflection;
    using Microsoft.FxCop.Sdk;

    /// <summary>
    /// A utility class for working with <see cref="AssemblyNode"/>s.
    /// </summary>
    public static class AssemblyNodeHelper
    {
        private static readonly IDictionary assemblyNodeCache = new Hashtable();

        #region Public Properties
        /// <summary>
        /// Gets the assembly node cache.
        /// </summary>
        /// <value>The assembly node cache.</value>
        /// <remarks>
        /// Used when calling <see cref="AssemblyNode.GetAssembly(string,IDictionary)"/> directly. Use <see cref="GetAssemblyNode"/> method instead.
        /// </remarks>
        public static IDictionary AssemblyNodeCache
        {
            get
            {
                return AssemblyNodeHelper.assemblyNodeCache;
            }
        }
        #endregion Public Properties

        #region Public Methods
        /// <summary>
        /// Gets the assembly node.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>The <see cref="AssemblyNode"/> for the given <paramref name="assembly"/>.</returns>
        public static AssemblyNode GetAssemblyNode(this Assembly assembly)
        {
            // Try to get the assembly node.
            var assemblyNode = AssemblyNode.GetAssembly(assembly.Location, AssemblyNodeHelper.AssemblyNodeCache);
            if (assemblyNode == null)
            {
                throw new InvalidFilterCriteriaException("Couldn't get the AssemblyNode for the specified assembly.");
            }

            return assemblyNode;
        }
        #endregion Public Methods
    }
}